package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    //运营数据统计
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception{

        Map<String,Object>map = new HashMap<>();
        //今日日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //这周周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //这月1号的日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());


        //今日新增会员数
        Integer todayNewMember = memberDao.findTodayNewMemberByDate(today);
        //会员总数
        Integer totalMember = memberDao.findMemberTotal();
        //这周新会员总数
        Integer thisWeekNewMember = memberDao.findThisWeekNewMemberAfterDate(thisWeekMonday);
        //这月新会员总数
        Integer thisMonthNewMember = memberDao.findThisMonthNewMemberAfterDate(firstDay4ThisMonth);

        //今日预约人数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        //今天到诊人数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //这周预约人数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //这周到诊人数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //这月预约人数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        //这月到诊人数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐,查询前四条
        List<Map<String, Object>> hotSetmeal = orderDao.findHotSetmeal();


        //封装数据
        map.put("reportDate",today);
        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",hotSetmeal);

        return map;
    }
}
